"use strict";

var insert = require("./insert.js");
const READ = require("./readConfig.js");
/*新增加文章*/
const doc_publish_sub = (req, res, next) => {
    let reqdata = appendEmptyValue(req);
    // console.log("inserCoanfig********")
    // console.log(reqdata)
    reqdata.author = "小贱", reqdata.author_id = "1";
    // insert.doc_publish_sub(reqdata,(err, data) => {
    //     if (err){
    //     	console.log("错误")
    //     	return　console.log(err);
    //     }

    //     let json = {
    //         code: 0,
    //         error: "",
    //         data: {
    //             doc_id:""
    //         }
    //     }
    //     // console.log(list);
    //     res.send(json);
    // })
    let doc_id = "";
    let save = insert.doc_publish_sub(reqdata);
    save.then(data => {
        console.log("存储成功");
        console.log(data);
        //return res.send(data);
        doc_id = data.doc_id;
        return doc_id;
    }).then(doc_id => {
        return READ.doc_doc_detail2(doc_id);
    }).then(list => {
        console.log("文章ID");
        //console.log(list.data.doc.shequ_id)
        res.send({ shequ_id: list.data.doc.shequ_id, doc_id: doc_id });
    }).catch(e => {
        // res.send(e)
        console.log(e);
    });
};

/*
	//替换空值或者undefined的value为 ""
*/
const appendEmptyValue = data => {
    for (let v in data) {
        if (!data[v]) {
            data[v] = "";
        }
    }
    return data;
};

module.exports = {
    doc_publish_sub
};